#!/bin/bash
{
	#////////////////////////////////////
	# DietPi Launcher
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Info:
	# - Location: /boot/dietpi/dietpi-launcher
	# - Whiptail menu with list of most DietPi programs
	#
	#////////////////////////////////////
	# Import DietPi-Globals --------------------------------------------------------------
	. /boot/dietpi/func/dietpi-globals
	readonly G_PROGRAM_NAME='DietPi-Launcher'
	G_CHECK_ROOT_USER
	# Import DietPi-Globals --------------------------------------------------------------

	#/////////////////////////////////////////////////////////////////////////////////////
	# MENUS
	#/////////////////////////////////////////////////////////////////////////////////////
	LAST_SELECTED_ITEM='DietPi-Config'

	Menu_Main()
	{
		G_WHIP_MENU_ARRAY=(

			'' '●─ Install Optimized Software '
			'DietPi-Software' ': Install optimised ready-to-run software'
			'' '●─ Configuration '
			'DietPi-Config' ': Feature rich config tool for your device'
			'DietPi-Drive_Manager' ': Setup and control multiple external drives'
			'DietPi-AutoStart' ': Choose what software runs after boot'
			'DietPi-Services' ': Service and process tool control'
			'DietPi-LED_control' ': Adjust board LED triggers'
			'DietPi-Cron' ': Modify the start times of cron jobs'
			'DietPi-JustBoom' ': Launch EQ and MPD audio options menu'
			'DietPi-CloudShell' ': Adjust update rate, scene settings and more'
			'' '●─ DietPi Updates '
			'DietPi-Update' ': Keep your DietPi system up to date'
			'' '●─ Backups / Sync '
			'DietPi-Backup' ': Backup and restore your DietPi system'
			'DietPi-Sync' ': Duplicate (sync) one directory to another'
			'' '●─ Maintenance '
			'DietPi-Explorer' ': File explorer and manager'
			'DietPi-Cleaner' ': Remove unwanted junk from your system'
			'' '●─ Misc '
			'DietPi-CPUinfo' ': Displays CPU temp, frequencies, type etc.'
			'DietPi-LetsEncrypt' ': Frontend for Lets Encrypt, free SSL certs'
			'DietPi-VPN' ': GUI to setup a VPN connection'
			'DietPi-DDNS' ': Manage domains for your dynamic IP'
			'DietPi-Survey' ': Join or leave our usage program, or view upload file'
			'DietPi-BugReport' ': Found a bug? Let us know!'
			'DietPi-MorseCode' ': Converts and outputs a text file to morsecode'

		)

		G_WHIP_DEFAULT_ITEM=$LAST_SELECTED_ITEM
		G_WHIP_BUTTON_CANCEL_TEXT='Exit'
		if G_WHIP_MENU 'Please select a program to run:'
		then
			[[ $G_WHIP_RETURNED_VALUE ]] || return

			LAST_SELECTED_ITEM=$G_WHIP_RETURNED_VALUE

			# Convert to lowercase
			local command=${G_WHIP_RETURNED_VALUE,,} error=0

			# Run
			if [[ -f '/boot/dietpi/'$command ]]
			then
				"/boot/dietpi/$command" || error=1

			# - different location
			elif [[ -f '/boot/dietpi/misc/'$command ]]
			then
				"/boot/dietpi/misc/$command" || error=1

			# - different location
			elif [[ -f '/boot/dietpi/func/'$command ]]
			then
				"/boot/dietpi/func/$command" || error=1
			else
				G_DIETPI-NOTIFY 1 "The chosen program $G_WHIP_RETURNED_VALUE could not be found on your system."
				read -rp " - Press any key to return to $G_PROGRAM_NAME..."
				return
			fi

			# Wait for user input
			[[ $error == 1 || $command == 'dietpi-cpuinfo' || $command == 'dietpi-update' || $command == 'dietpi-morsecode' ]] && read -rp "
- Press any key to return to $G_PROGRAM_NAME..."

		else
			G_WHIP_SIZE_X_MAX=50
			G_WHIP_YESNO "Exit $G_PROGRAM_NAME?" && exit 0
		fi
	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Main Loop
	#/////////////////////////////////////////////////////////////////////////////////////
	while :
	do
		G_TERM_CLEAR
		Menu_Main
	done
	#-------------------------------------------------------------------------------------
	exit
	#-------------------------------------------------------------------------------------
}
